/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author haoliang.donghl
 */
public class HttpConnection extends Thread{
    private static HttpConnection connection;
    private static final byte CR = (byte)'\r';
    private static final byte LF = (byte)'\n';
    private static final byte[] CRLF = new byte[]{CR,LF};
    private ServerSocket serverSocket;

    class ServerThread extends Thread{
        Socket socket;

        public ServerThread(Socket socket){
            this.socket = socket;
        }

        private String getContent(){
            StringBuffer sb = new StringBuffer("");
            for (Machine machine : Machine.allMachines){
                sb.append("<Machine id=\"1\">");
                ArrayList<Instance> instances = machine.getRunningApps();
                for (Instance instance : instances){
                    sb.append("<App id=\"" + instance.app.getId() + "\" ");
                    sb.append("load=" + instance.getLoad() + " />");
                }
                sb.append("</Machine>");
            }
            return sb.toString();
        }

        @Override
        public void run(){
            try {
                BufferedReader netIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                OutputStream netOut = socket.getOutputStream();
                String content = getContent();
                netOut.write("HTTP/1.1 200 OK".getBytes());
                netOut.write(CRLF);
                netOut.write("Server: Apache/1.3.31 (Unix) mod_throttle/3.1.2".getBytes());
                netOut.write(CRLF);
                netOut.write("Cache-Control: no-cache".getBytes());
                netOut.write(CRLF);
                netOut.write("Pragma: no-cache".getBytes());
                netOut.write(CRLF);
                netOut.write("Accept-Ranges: bytes".getBytes());
                netOut.write(CRLF);
                netOut.write("Connection: close".getBytes());
                netOut.write(CRLF);
                netOut.write("Content-Type: text/html".getBytes());
                netOut.write(CRLF);
                netOut.write("Content-Length: ".getBytes());
                netOut.write(String.valueOf(content.length()).getBytes());
                netOut.write(CRLF);
                netOut.write(CRLF);
                netOut.write(content.getBytes());
                netOut.write(CRLF);
                netOut.flush();
                netIn.close();
                netOut.close();
            } catch (IOException ex) {
                Logger.getLogger(HttpConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static HttpConnection getInstance(){
        if (connection == null) connection = new HttpConnection();
        return connection;
    }

    private HttpConnection(){

    }

    @Override
    public void run(){
        try {
            serverSocket = new ServerSocket(80);
            while (true){
                Socket socket = serverSocket.accept();
                ServerThread st = new ServerThread(socket);
                st.start();
            }
        } catch (IOException ex) {
            Logger.getLogger(HttpConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        getInstance().start();
    }
}
